<?php

namespace app\api\controller;

use app\payment\PayFactory;
use app\services\PaymentService;
use app\services\TransferService;
use support\Request;
use app\services\UserCompanyService;

class UserCompanyController
{
    public function getCompanyList(Request $request)
    {
        try {
            $page = (int)$request->input('page', 1);
            $pageSize = (int)$request->input('page_size', 10);
            $keyword = $request->input('keyword', null);

            $data = UserCompanyService::getCompanyList($page, $pageSize, $keyword);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function getCompanyDetail(Request $request)
    {
        try {
            $id = (int)$request->input('id', 1);

            $data = UserCompanyService::getCompanyDetail($id);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function createOrder(Request $request)
    {
        try {
            $productId = $request->post('product_id');
            $payWay = $request->post('pay_way', PayFactory::WECHAT);
            $payWayType = $request->post('pay_way_type');

            if (PaymentService::needCheckPaymentPassword($payWay)) {
                PaymentService::checkPayPassword($request->post('pay_password'));
            }


            $order = UserCompanyService::createOrder($productId, $payWay, $payWayType);

            return ok_rsp('成功', $order);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function getCompanyProducts(Request $request)
    {
        try {
            $companyId = (int)$request->input('company_id');
            $page = (int)$request->input('page', 1);
            $pageSize = (int)$request->input('page_size', 10);
            $keyword = $request->input('keyword', null);

            $data = UserCompanyService::getCompanyProducts($companyId, $page, $pageSize, $keyword);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function getCompanyProduct(Request $request)
    {
        try {
            $id = (int)$request->input('id');

            $data = UserCompanyService::getCompanyProduct($id);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function batchAddProducts(Request $request)
    {
        try {
            $products = $request->input('products');

            UserCompanyService::batchAddProducts($products);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function getProductList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $keyword = $request->post('keyword', null);
            $fileType = $request->post('file_type', 'all');

            $data = UserCompanyService::getProductList($page, $length, $keyword, $fileType);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function editProduct(Request $request)
    {
        try {
            $id = (int)$request->input('id');
            $data = $request->input('data');

            UserCompanyService::editProduct($id, $data);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function delProduct(Request $request)
    {
        try {
            $id = (int)$request->input('id');

            UserCompanyService::delProduct($id);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function searchProductsByCompanyAndFileName(Request $request)
    {
        try {
            $companyName = $request->input('company_name');
            $fileName = $request->input('file_name');
            $page = (int)$request->input('page', 1);
            $pageSize = (int)$request->input('page_size', 10);

            $data = UserCompanyService::searchProductsByCompanyAndFileName($companyName, $fileName, $page, $pageSize);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function searchProductsByCompanyIdAndFileName(Request $request)
    {
        try {
            $companyId = (int)$request->input('company_id');
            $fileName = $request->input('file_name');
            $fileType = $request->input('file_type');
            $page = (int)$request->input('page', 1);
            $pageSize = (int)$request->input('page_size', 10);

            print_r($request->all());

            $data = UserCompanyService::searchProductsByCompanyIdAndFileName($companyId, $fileName, $fileType, $page, $pageSize);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }
}